package cc.booku.owlbox.AliPay;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.alibaba.fastjson.JSONObject;
import com.sunfusheng.marqueeview.MarqueeView;

import java.util.ArrayList;
import java.util.List;

import cc.booku.owlbox.ArtDesign.SystemUtil;
import cc.booku.owlbox.FileOperations.FileoOperations;
import cc.booku.owlbox.R;
import cc.booku.owlbox.UserSettingsPage.CodeCreator;

public class PayActivity extends AppCompatActivity {

    private Toolbar Toolbar;
    private TextView bt_gopay;
    private ImageView iv_icon;
    private MarqueeView tv_thanksdonating;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        bt_gopay = (TextView) findViewById(R.id.bt_gopay);
        iv_icon = (ImageView) findViewById(R.id.iv_icon);
        SystemUtil.setStatusBarColor(this, Color.parseColor("#448aca"));
        LoadToolbar();
        bt_gopay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goalipay();
            }
        });

        Bitmap bitmap = null;
        //可对图片进行修改
        Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        bitmap = CodeCreator.createQRCode("https://qr.alipay.com/fkx00671y2pxjvug8rdup1b", 400, 400, logo);
        iv_icon.setImageBitmap(bitmap);


        tv_thanksdonating = (MarqueeView) findViewById(R.id.tv_thanksdonating);
        String BillBoard_V = FileoOperations.readAssetsTxt(PayActivity.this, "TxtData/BillBoard");
        JSONObject objsd = JSONObject.parseObject(BillBoard_V);
        String result1 = objsd.getString("Donating1");
        String result2 = objsd.getString("Donating2");
        String result3 = objsd.getString("Donating3");
        String result4 = objsd.getString("Donating4");
        List<String> messages = new ArrayList<>();
        messages.add(result1);
        messages.add(result2);
        messages.add(result3);
        messages.add(result4);
        //  Tv_label.startWithList(messages);


// 在代码里设置自己的动画
        tv_thanksdonating.startWithList(messages, R.anim.anim_bottom_in, R.anim.anim_top_out);
    }

    private void LoadToolbar() {
        //新页面接收数据


        Toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(Toolbar);//利用Toolbar代替ActionBar
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到用户设置页面
                finish();
            }
        });
    }

    private void goalipay() {

        if (RomUtils.checkApkExist(PayActivity.this, "com.eg.android.AlipayGphone")) {
            donate(PayActivity.this);
        } else {
            Toast.makeText(PayActivity.this, "本机未安装支付宝", Toast.LENGTH_SHORT).show();
        }

    }

    //https://qr.alipay.com/fkx00671y2pxjvug8rdup1b
    //跳转到支付宝付款界面
    private static void donate(Context context) {
        String intentFullUrl = "intent://platformapi/startapp?saId=10000007&" +
                "clientVersion=3.7.0.0718&qrcode=https%3A%2F%2Fqr.alipay.com%2Ffkx00671y2pxjvug8rdup1b%3F_s" +  //这里把｛URLcode｝替换成第一步扫描的结果
                "%3Dweb-other&_t=1472443966571#Intent;" +
                "scheme=alipayqr;package=com.eg.android.AlipayGphone;end";
        try {
            Intent intent = Intent.parseUri(intentFullUrl, Intent.URI_INTENT_SCHEME);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
